﻿using OpenTK.Graphics;
using OpenTK.Math;
using System.Drawing;
using Rekx.Graphics;

namespace Rekx.Pong
{
    class Paddle
    {
        // This is one-dimensional only
        private float positionX = 0;
        private float posZ = 0;
        private float width = 4;
        private float speed = 0.1f;

        // use drawable component
        // this actually leaks, because the pad does not delete the component
        DrawableComponent drawComponent = null;


        public float PositionX
        {
            get
            {
                return positionX;
            }
        }


        public DrawableComponent DrawComponent
        {
            get
            {
                return drawComponent;
            }
        }

        public Paddle(float posZ, float speed)
        {
            this.posZ = posZ;
            this.speed = speed;


            SetupDrawComponent();

        }

        public Paddle(float posZ)
        {
            this.posZ = posZ;
            SetupDrawComponent();
        }

        private void SetupDrawComponent()
        {
            Vector3[] vertices = new Vector3[]
            {
                new Vector3(positionX - width / 2, 0.0f, 0.25f),
                new Vector3(positionX + width/2, 0.0f, 0.25f),
                new Vector3(positionX - width / 2, 0.0f, -0.25f),
                new Vector3(positionX + width / 2, 0.0f, -0.25f)
            };

            int[] indices = new int[]
            {
                0,
                1,
                2,
                3
            };

            drawComponent = new DrawableComponent(vertices, indices);
        }

        public void MoveLeft()
        {
            if (positionX <= -10 + width / 2)
            {
                positionX = -10 + width / 2;
                return;
            }
            positionX -= speed;

        }
        public void MoveRight()
        {
            if (positionX >= 10 - width / 2)
            {
                positionX = 10 - width / 2;
                return;
            }
            positionX += speed;
        }


        public bool CollidesWithBall(Ball ball)
        {
            if (ball.Position.X >= positionX - width / 2 && ball.Position.X <= positionX + width / 2)
            {
                ball.velocity *= 1.1f;
                return true;
            }

            return false;

        }

        /// <summary>
        /// This is the AI function
        /// </summary>
        /// <param name="ball">Ball to follow</param>
        public void MoveSelfAccordingToBall(Ball ball)
        {

            float saveSpeed = speed;
            // use random number to make AI make changes in speed
            // this should make it fail at times
            // the higher the interval, the higher the fail rate will be
            System.Random rand = new System.Random();
            //speed = speed / (rand.Next(1, 20)*0.2f);
            if (true)
            {
                if (ball.Position.X > positionX+speed)
                {
                    MoveRight();
                }
                else if (ball.Position.X < positionX-speed)
                {
                    MoveLeft();
                }
            }

            speed = saveSpeed;
        }
    }
}
